<?

include('../base.php');
User::protect();
$version = "1.5";

if ($_REQUEST['upgrade']) { 
	
	// ################## Upgrade Code for Audition 1.2 ##################
	
	$q = "show columns from Release_Tracks";
	$r = @mysql_query($q);
	
	$doAlter = true;
	$message = null;
	while ($row = @mysql_fetch_assoc($r)) {
		if ($row['Field'] == 'av_area_id') {
			$doAlter = false;
			$message[] = "Release tracks already contains av_area_id column. No change made.";
		}
	}
	
	if ($doAlter) {
		$q = "alter table Release_Tracks add column av_area_id mediumint(9) not null default 0";
		$r = @mysql_query($q);
		if (!$r) {
			$message[] = "Error: There was problem when attempting to add av_area_id column to table Release_Tracks.";
		} else {
			$message[] = "Added av_area_id column to table Release_Tracks.";
		}
	}
	
	// create config table
	$doAlter = true;
	$q = "show columns from Config";
	$r = @mysql_query($q);
	$rows = @mysql_num_rows($r);
	if ($rows > 0) {
		$doAlter = false;
		$message[] = "Config table already exists. No change made.";
	}
	
	if ($doAlter) {
		$q = "CREATE TABLE Config (
	  name varchar(255) NOT NULL default '',
	  value varchar(255) NOT NULL default '',
	  PRIMARY KEY  (name))";
	  $r = @mysql_query($q);
		if (!$r) {
			$message[] = "Error: There was problem when attempting to create the Config table.";
		} else {
			$message[] = "Config table created.";
		}
	}
	
	// modify Darkroom_Media_to_Areas
	
	$q = "show columns from DarkRoom_Media_to_Areas";
	$r = @mysql_query($q);
	
	$protectedFile = true;
	$access = true;
	while ($row = @mysql_fetch_assoc($r)) {
		if ($row['Field'] == 'protected_filename') {
			$protectedFile = false;
			$message[] = "DarkRoom_Media_to_Areas already contains protected_filename column. No change made.";
		}
		if ($row['Field'] == 'access') {
			$access = false;
			$message[] = "DarkRoom_Media_to_Areas already contains access column. No change made.";
		}
	}
	
	if ($access) {
		$q = "alter table DarkRoom_Media_to_Areas add column access varchar(255) NOT NULL default 'DOWNLOAD'";
		$r = @mysql_query($q);
		if (!$r) {
			$message[] = "Error: There was problem when attempting to add access column to table DarkRoom_Media_to_Areas.";
		} else {
			$message[] = "Added access column to table DarkRoom_Media_to_Areas.";
		}
	}
	
	if ($protectedFile) {
		$q = "alter table DarkRoom_Media_to_Areas add column protected_filename varchar(255) default NULL";
		$r = @mysql_query($q);
		if (!$r) {
			$message[] = "Error: There was problem when attempting to add protected_filename column to table DarkRoom_Media_to_Areas.";
		} else {
			$message[] = "Added protected_filename column to table DarkRoom_Media_to_Areas.";
		}
	}
	
	// ################## End Upgrade Code for Audition 1.2 ##################

	// ################## Upgrade Code for Audition 1.5 ##################
	
	$q = "show columns from Links_Categories";
	$r = @mysql_query($q);
	$rows = @mysql_num_rows($r);
	if ($rows > 0) {
		$doAlter = false;
		$message[] = "Links_Categories table already exists. No change made.";
	} else {
		
		$q = "CREATE TABLE Links_Categories (
	  ID mediumint(5) NOT NULL auto_increment,
	  name varchar(255) NOT NULL default '',
	  description mediumtext,
	  parent_id mediumint(5) NOT NULL default '0',
	  is_active tinyint(1) NOT NULL default '0',
	  PRIMARY KEY  (ID))";
	  
		$r = mysql_query($q);
		if (!$r) {
			$message[] = "Error: There was a problem adding the link categories table.";
		} else {
			$message[] = "Added Links_Categories table.";
		}

		$q = "INSERT INTO Links_Categories(name, is_active) VALUES ('Unnamed Category', 1)";
		$r = mysql_query($q);
		if (!$r) {
			$message[] = "Error: There was a problem inserting a category into the Links_Categories table.";
		} else {
			$message[] = "Inserted the Unnamed Category into the Links_Categories table.";
		}
	}
	
  	$q = "alter table Links add column category_id mediumint(5) not null default 0";
	$r = @mysql_query($q);
	if (!$r) {
		$message[] = "Error: There was problem when attempting to add the category id field to the links table.";
	} else {
		$message[] = "Added category ID to links table.";
	}

  	$q = "UPDATE Links SET category_id=1 WHERE category_id=0";
	$r = @mysql_query($q);
	if (!$r) {
		$message[] = "Error: There was problem when attempting to set the category id field to the unnamed category in the Links table.";
	} else {
		$message[] = "Set category ID in Links table.";
	}

  	$q = "ALTER TABLE Band_Information ADD COLUMN miscellaneous MEDIUMTEXT";
	$r = @mysql_query($q);
	if (!$r) {
		$message[] = "Error: There was problem when attempting to add the miscellaneous field to the Band_Information table.";
	} else {
		$message[] = "Added miscellaneous to Band_Information table.";
	}

  	$q = "ALTER TABLE Band_Information ADD COLUMN description MEDIUMTEXT";
	$r = @mysql_query($q);
	if (!$r) {
		$message[] = "Error: There was problem when attempting to add the description field to the Band_Information table.";
	} else {
		$message[] = "Added description to Band_Information table.";
	}
}
$layoutPrefix = "../";
include(BASE_FULL_DIRECTORY . AUDITION_WEB_DIRECTORY . '/layout/header.php');
?>

<div id="breadcrumb">
	Audition Installation &#62;
</div>
<h1>upgrading audition:</h1>
<div class="inset">
<? if ($_REQUEST['upgrade']) { ?>
<ul>
<? foreach($message as $item) { ?>
	<li><?=$item?></li>
<? } ?>
</ul>
<strong>Upgrade complete!</strong>
<? } else { ?>

<form method="post" action="upgrade.php">
<input type="submit" name="upgrade" value="Upgrade to Audition <?=$version?> &gt;" />
</form>

<? } ?>
</div>

<? include(BASE_FULL_DIRECTORY . AUDITION_WEB_DIRECTORY . '/layout/footer.php'); ?>
